package net.hserver.apm.util;

import net.hserver.apm.bean.LinkData;

import java.util.ArrayList;
import java.util.List;

public class LinkTree {
    private List<LinkData> menuList = new ArrayList<LinkData>();
    public LinkTree(List<LinkData> menuList) {
        this.menuList=menuList;
    }

    //建立树形结构
    public LinkData builTree(){
           return buildChilTree(getRootNode());
    }

    //递归，建立子树形结构
    private LinkData buildChilTree(LinkData pNode){
        List<LinkData> chilMenus =new ArrayList<>();
        for(LinkData menuNode : menuList) {
            if(menuNode.getpSpanId().equals(pNode.getSpanId())) {
                chilMenus.add(buildChilTree(menuNode));
            }
        }
        pNode.setChildren(chilMenus);
        return pNode;
    }

    //获取根节点
    private LinkData getRootNode() {
        List<LinkData> rootMenuLists =new  ArrayList<LinkData>();
        for(LinkData menuNode : menuList) {
            if (menuNode.getpSpanId().equals("0")) {
                return menuNode;
            }

        }
        return null;
    }
}